<template>
  <!-- 学校校区管理 -->
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card" style="margin-top: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="buttonMethod(1)"
            v-hasPermi="['systemSet:banner:add']"
            >新增
          </el-button>
        </el-col>

        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <el-card class="table-card" style="margin-top: 10px">
      <el-table :data="dataList" row-key="id" stripe border>
        <el-table-column label="图片" prop="bannerPath">
          <template slot-scope="scope">
            <img
              :src="scope.row.bannerPath"
              @click="lookImg(scope.row.bannerPath)"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(2, scope.row)"
              v-hasPermi="['systemSet:banner:del']"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :title="title" :visible.sync="status" width="40%" :before-close="handleClose" >
      <el-form label-width="160px" label-position="right" size="small" class="formBox">
        <el-row class="mb_10">
          <el-col :span="20">
            <el-form-item label="上传图片：" class="is-required">
              <image-upload
                @success="fileSuccess"
                v-model="queryForm.fileName"
                action="/system/bannerResult/upload"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>
<script>
import { bannerResult, bannerDelete } from "@/api/systemSet";
export default {
  name: "",
  data() {
    return {
      queryForm: {},
      dataList: [],
      title: "",
      status: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      showSearch: true,
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    getList() {
      var that = this;
      bannerResult().then((res) => {
        if (res.code == 200) {
          that.dataList = res.rows;
          that.total = res.total;
        }
      });
    },
    /**
     * 按钮点击事件处理方法
     */
    buttonMethod(options, value) {
      // 修改和添加
      if (options == 1) {
        this.queryForm = {};
        this.title = "添加";
        this.status = true;
      }
      // 删除
      else if (options == 2) {
        this.$confirm("确定删除吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          bannerDelete({ id: value.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.resetQuery();
            } else {
              this.$message({
                type: "error",
                message: "删除失败!",
              });
            }
          });
        });
      }
    },
    // 按钮关闭
    handleClose(){
      this.status = false;
      this.getList();
    },
    fileSuccess() {
      this.status = false;
      this.getList();
    },
    lookImg(url) {
      this.dialogImageUrl = url;
      this.dialogVisible = true;
    },
    /**
     * 处理查询请求
     *
     * 将查询参数中的页码重置为1，并调用getList方法获取列表数据
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.resetForm("queryForm");
      this.date = [];
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.handleQuery();
    },
  },
};
</script>
<style>
.width_100 {
  width: 100%;
}
</style>
